package com.bigchun.seeland;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import bean.ContentDataBean;
import util.SystemStatusManager;

public class BrandActivity extends BaseActivity {


    private LinearLayout ll_reback;
    private LinearLayout ll_xiran;
    private LinearLayout ll_weiran;
    private LinearLayout ll_mutou;
    private ImageView img_xiran;
    private ImageView img_weiran;
    private ImageView img_mutou;
    private String apptype;
    private String textData;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_selectbrand);
        setTranslucentStatus();
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String token = sharedPreferences.getString("token", "");
        apptype = sharedPreferences.getString("apptype", "0");
        textData = sharedPreferences.getString("textData", "");
        initView();
        initLin();


    }


    private void initView() {
        TextView tv_title = findViewById(R.id.tv_title);
        TextView tv_xiran = findViewById(R.id.tv_xiran);
        TextView tv_wood = findViewById(R.id.tv_wood);
        ContentDataBean.DataBean textContent = new Gson().fromJson(textData, new TypeToken<ContentDataBean.DataBean>() {}.getType());
        if (textContent != null){
            tv_title.setText(textContent.getSelectbrand());
            tv_xiran.setText(textContent.getXiran());
            tv_wood.setText(textContent.getWood());
        }

        ll_reback = findViewById(R.id.ll_reback);
        ll_xiran = findViewById(R.id.ll_xiran);
        ll_weiran = findViewById(R.id.ll_weiran);
        ll_mutou = findViewById(R.id.ll_mutou);
        img_xiran = findViewById(R.id.img_xiran);
        img_weiran = findViewById(R.id.img_weiran);
        img_mutou = findViewById(R.id.img_mutou);

        if ("0".equals(apptype)){
            img_weiran.setImageResource(R.mipmap.icon_nocheck);
            img_xiran.setImageResource(R.mipmap.icon_nocheck);
            img_mutou.setImageResource(R.mipmap.icon_checkd);
            ll_xiran.setTag("false");
            ll_mutou.setTag("true");
            ll_weiran.setTag("false");
        }else if ("1".equals(apptype)){
            img_weiran.setImageResource(R.mipmap.icon_nocheck);
            img_mutou.setImageResource(R.mipmap.icon_nocheck);
            img_xiran.setImageResource(R.mipmap.icon_checkd);
            ll_xiran.setTag("true");
            ll_mutou.setTag("false");
            ll_weiran.setTag("false");
        }

    }


    private void initLin() {
        ll_reback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ll_xiran.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tag = (String) ll_xiran.getTag();
                if ("true".equals(tag)){
                    //说明上次选中的已经是熙然，无需再选中
                }else{
                    //说明之前选中的不是熙然，是其他品牌
                    ll_xiran.setTag("true");
                    ll_mutou.setTag("false");
                    ll_weiran.setTag("false");

                    sharedPreferences.edit().putString("apptype","1").commit();
                    img_weiran.setImageResource(R.mipmap.icon_nocheck);
                    img_mutou.setImageResource(R.mipmap.icon_nocheck);
                    img_xiran.setImageResource(R.mipmap.icon_checkd);
                    Intent intent = new Intent(BrandActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);



//                    PackageManager packageManager = getPackageManager();
//                    packageManager.setComponentEnabledSetting(new ComponentName(BrandActivity.this,
//                            "com.bigchun.seeland.SplashActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
//                    packageManager.setComponentEnabledSetting(new ComponentName(BrandActivity.this, getPackageName() +
//                            ".XiRan"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                }

            }
        });
        ll_mutou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tag = (String) ll_mutou.getTag();
                if ("true".equals(tag)){
                    //说明上次选中的已经是木头，无需再选中
                }else {
                    //说明之前选中的不是木头，是其他品牌
                    ll_xiran.setTag("false");
                    ll_mutou.setTag("true");
                    ll_weiran.setTag("false");

                    sharedPreferences.edit().putString("apptype", "0").commit();
                    img_weiran.setImageResource(R.mipmap.icon_nocheck);
                    img_xiran.setImageResource(R.mipmap.icon_nocheck);
                    img_mutou.setImageResource(R.mipmap.icon_checkd);
                    Intent intent = new Intent(BrandActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);

//                    PackageManager packageManager = getPackageManager();
//                    packageManager.setComponentEnabledSetting(new ComponentName(BrandActivity.this,
//                            "com.bigchun.seeland.SplashActivity"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager
//                            .DONT_KILL_APP);
//                    packageManager.setComponentEnabledSetting(new ComponentName(BrandActivity.this, getPackageName() +
//                            ".XiRan"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager
//                            .DONT_KILL_APP);

                }
            }
        });
        ll_weiran.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                img_weiran.setImageResource(R.mipmap.icon_nocheck);
                img_xiran.setImageResource(R.mipmap.icon_nocheck);
                img_mutou.setImageResource(R.mipmap.icon_nocheck);
            }
        });
    }
    /**
     * 设置状态栏背景状态
     */
    public  void setTranslucentStatus()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window win =  getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }
        SystemStatusManager tintManager = new SystemStatusManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(0);//状态栏无背景
    }


}
